package com.hnas.sys.service.system;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.hnas.core.util.DateUtil;
import com.hnas.sys.dao.FrontUserDAO;
import com.hnas.sys.domain.ResourceStatus;
import com.hnas.sys.orm.FrontUser;
import com.hnas.sys.service.system.dto.DTOUser;
@Component("frontUserService")
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public class FrontUserService implements IFrontUserService {
	@Autowired
	private FrontUserDAO frontUserDAO;
	public long updateFrontUser(DTOUser user) {
		// TODO Auto-generated method stub
//		return 0;
		if (null==user)
			return 0;
		String hql = "from FrontUser t where t.account=?1";
		List<FrontUser>users = this.frontUserDAO.findByQuery(hql, user.getAccount());
		if (users.size()>0){
			FrontUser u = users.get(0);
			u.setLastLogin(DateUtil.dateTimeToString(new Date()));
			this.frontUserDAO.merge(u);
			return u.getId();
		}else{
			FrontUser u = new FrontUser();
			u.setAccount(user.getAccount());
			u.setTelephone(user.getTelephone());
			u.setName(user.getAccount());
			u.setStatus(ResourceStatus.VALID);
			u.setLastLogin(DateUtil.dateTimeToString(new Date()));
			u.setUpdateTime(DateUtil.dateTimeToString(new Date()));
//			u.setUserType(UserType.CUSTOMER);
			this.frontUserDAO.persist(u);
			return u.getId();
		}
	}

}
